const express = require('express');

const router = express.Router();
//导入数据库操作模块
const db = require('../db/index');

//获取所有banner
router.get('/list', (req, res) => {
    db.query(`select * from banner order by status desc,priority asc`, (err, result) => {
        if (err) {
            console.log(err);
            res.json({
                code: 500,
                message: '服务器错误'
            });

        } else {
            res.json({
                code: 200,
                message: '获取成功',
                list: result
            });
        }
    })
});

//创建新轮播图
router.post('/insert', (req, res) => {
    const text = req.body.text;
    const pic = req.body.pic;
    const path = req.body.path;
    const status = req.body.status;
    const priority = req.body.priority;

    const banner = {
        text,
        pic,
        path,
        status,
        priority
    }

    //创建
    db.query(`insert into banner set ?`, banner, (err, result) => {
        if (err) {
            console.log(err);
            res.json({
                code: 500,
                message: '服务器错误'
            });

        } else {
            res.json({
                code: 200,
                message: '创建成功'
            });
        }
    })

});

//修改轮播图状态
router.post('/updateStatus', (req, res) => {
    const id = req.body.id;
    const status = req.body.status;

    //获取status为1的banner数
    if (status == 1) {
        db.query(`select count(*) as count from banner where status = 1`, (err, result) => {
            if (err) {
                console.log(err);
                return res.json({
                    code: 500,
                    message: '服务器错误'
                });
            } else {
                if (result[0].count > 10) {
                    return res.json({
                        code: 500,
                        message: '最多只能有10张轮播图为显示状态'
                    });
                }
            }
        })
    }
    db.query(`update banner set status = ${status} where id = ${id}`, (err, result) => {
        if (err) {
            console.log(err);
            return res.json({
                code: 500,
                message: '服务器错误'
            });
        } else {
            return res.json({
                code: 200,
                message: '操作成功'
            });
        }
    })
})

//编辑
router.post('/edit', (req, res) => {
    const id = req.body.id;
    const text = req.body.text;
    const pic = req.body.pic;
    const path = req.body.path;
    const priority = req.body.priority;
    const status = req.body.status;

    const banner = {
        text,
        pic,
        path,
        priority,
        status
    }

    db.query(`update banner set ? where id = ${id}`, banner, (err, result) => {
        if (err) {
            console.log(err);
            res.json({
                code: 500,
                message: '服务器错误'
            });
        } else {
            res.json({
                code: 200,
                message: '操作成功'
            });
        }
    })
})


//删除
router.post('/delete', (req, res) => {
    const id = req.body.id;
    //删除轮播图
    db.query(`delete from banner where id = ${id}`, (err, result) => {
        if (err) {
            res.json({
                code: 500,
                message: '服务器错误'
            });
        } else {
            res.json({
                code: 200,
                message: '删除成功'
            });
        }
    });
});

module.exports = router;